﻿#include "msgdialog.h"
#include <QPainter>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QTimer>

MsgDialog::MsgDialog(QWidget *parent)
    : QDialog(parent), backgroundColor(Qt::white), backgroundOpacity(1.0),
      fontColor(Qt::black), fontSize(12), cornerRadius(10), borderTransparent(false),
      iconPosition(IconLeft) {
    // 设置窗口无边框
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    setAttribute(Qt::WA_TranslucentBackground);

    // 创建控件
    iconLabel = new QLabel(this);
    titleLabel = new QLabel(this);
    contentLabel = new QLabel(this);

    iconLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setAlignment(Qt::AlignCenter);
    contentLabel->setAlignment(Qt::AlignCenter);

    // 创建主布局
    mainLayout = new QVBoxLayout(this);
    updateLayout();

    // 初始化定时器
    closeTimer = new QTimer(this);
    connect(closeTimer, &QTimer::timeout, this, &MsgDialog::closeDialog);
}

void MsgDialog::setTitle(const QString &title) {
    this->title = title;
    titleLabel->setText(title);
}

void MsgDialog::setContent(const QString &content) {
    this->content = content;
    contentLabel->setText(content);
}

void MsgDialog::setBackgroundColor(const QColor &color) {
    this->backgroundColor = color;
    update();
}

void MsgDialog::setBackgroundOpacity(float opacity) {
    this->backgroundOpacity = opacity;
    setWindowOpacity(opacity);
    update();
}

void MsgDialog::setFontColor(const QColor &color) {
    this->fontColor = color;
    QList<QLabel *> labels = findChildren<QLabel *>();
    for (QLabel *label : labels) {
        QPalette palette = label->palette();
        palette.setColor(QPalette::WindowText, color);
        label->setPalette(palette);
    }
}

void MsgDialog::setFontSize(int size) {
    this->fontSize = size;
    QList<QLabel *> labels = findChildren<QLabel *>();
    for (QLabel *label : labels) {
        QFont font = label->font();
        font.setPointSize(size);
        label->setFont(font);
    }
}

void MsgDialog::setCornerRadius(int radius) {
    this->cornerRadius = radius;
    update();
}

void MsgDialog::setBorderTransparent(bool transparent) {
    this->borderTransparent = transparent;
    if (transparent) {
        setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    } else {
        setWindowFlags(Qt::Dialog);
    }
    update();
}

void MsgDialog::setAutoCloseTimeout(int milliseconds) {
    if (milliseconds > 0) {
        closeTimer->start(milliseconds); // 启动定时器
    }
}

void MsgDialog::setIcon(const QIcon &icon) {
    this->icon = icon;
    iconLabel->setPixmap(icon.pixmap(48, 48)); // 设置图标大小为 48x48
}

void MsgDialog::setIconPosition(IconPosition position) {
    this->iconPosition = position;
    updateLayout(); // 更新布局
}

void MsgDialog::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制背景
    QRect rect = this->rect();
    QPainterPath path;
    path.addRoundedRect(rect, cornerRadius, cornerRadius);
    painter.fillPath(path, backgroundColor);

    // 调用基类的绘制事件
    QDialog::paintEvent(event);
}

void MsgDialog::closeDialog() {
    close(); // 关闭对话框
}

void MsgDialog::updateLayout() {
    // 清空布局
    QLayoutItem *item;
    while ((item = mainLayout->takeAt(0)) != nullptr) {
        delete item;
    }

    // 根据图标位置设置布局
    switch (iconPosition) {
    case IconLeft: {
        QHBoxLayout *hLayout = new QHBoxLayout();
        hLayout->addWidget(iconLabel);
        hLayout->addWidget(titleLabel);
        hLayout->addWidget(contentLabel);
        mainLayout->addLayout(hLayout);
        break;
    }
    case IconRight: {
        QHBoxLayout *hLayout = new QHBoxLayout();
        hLayout->addWidget(titleLabel);
        hLayout->addWidget(contentLabel);
        hLayout->addWidget(iconLabel);
        mainLayout->addLayout(hLayout);
        break;
    }
    case IconTop: {
        mainLayout->addWidget(iconLabel);
        mainLayout->addWidget(titleLabel);
        mainLayout->addWidget(contentLabel);
        break;
    }
    case IconBottom: {
        mainLayout->addWidget(titleLabel);
        mainLayout->addWidget(contentLabel);
        mainLayout->addWidget(iconLabel);
        break;
    }
    }
}
